
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/dist/query/react";

//入库操作请求API
export const accountApi = createApi({
    reducerPath: 'account',
    //设置基本请求路径
    baseQuery: fetchBaseQuery({
        baseUrl: window.app,
        credentials: 'include', 
        // baseQueryFn: async (args, api, extraOptions) => {
        //     // 获取Cookie
        //     const cookies = document.cookie;
      
        //     // 设置请求标头
        //     extraOptions.headers = {
        //       ...extraOptions.headers,
        //       'Cookie': cookies,
        //     };
      
        //     // 发送请求
        //     const result = await fetch(args.url, extraOptions);
        //     console.log(cookies);
      
        //     // 返回响应
        //     return result.json();
        //   },
    }),
    tagTypes:['account'],//定义标签
    endpoints(build) {
        return {
            //获取入库记录
            getaccount: build.query({
                query:(para) =>({
                    url: 'account/getAccount',
                    method:'GET',
                    params:para,
                }),
                transformResponse(baseQuery){
                    return baseQuery;

                },
                providesTags:['account']//定义标签，如果该标签失效则会触发重新访问服务器
            }),
           
            //添加一条商品入库记录
            postaddaccount: build.mutation({
                query(data) {
                    return {
                        url: 'account/addAccount',
                        method: 'POST',
                        body:data
                    };
                },
                transformResponse(baseQuery){
                    return baseQuery.data;

                },
                invalidatesTags:['account']
            }),
            //修改一条商品入库记录
            puteditaccount: build.mutation({
                query(data) {
                    return {
                        url: 'account/editAccount',
                        method: 'PUT',
                        body:data
                    };
                },
                transformResponse(baseQuery){
                    return baseQuery.data;

                },
                invalidatesTags:['account']//定义标签，一旦修改数据，则会使该标签失效
            }),
            puteditavater: build.mutation({
                query(data) {
                    return {
                        url: 'account/editAvater',
                        method: 'PUT',
                        body:data
                    };
                },
                transformResponse(baseQuery){
                    return baseQuery.data;

                },
                invalidatesTags:['account']//定义标签，一旦修改数据，则会使该标签失效
            }),
            //删除一条商品入库记录
            deleteaccount: build.mutation({
                query(key) {
                    return {
                        url: `account/delAccount?key=${key}`,
                        method: 'DELETE',
                        
                    };
                },
                transformResponse(baseQuery){
                    return baseQuery.data;

                },
                invalidatesTags:['account']//定义标签，一旦修改数据，则会使该标签失效
            }),
        }
    }
});


//导出钩子函数给外部使用，自动生成hooks的名称为`use(Endpoint_name)Query`
export const { useGetaccountQuery,usePostaddaccountMutation,usePuteditaccountMutation,usePuteditavaterMutation,useDeleteaccountMutation } = accountApi;